package com.test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.*;
public class MyTankGame1 extends JFrame {
	MyPanel mp = null;
	public MyTankGame1() {
		mp=new MyPanel();
		this.addKeyListener(mp);
		this.add(mp);
		this.setSize(400,300);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		MyTankGame1 myTankGame1 = new MyTankGame1();
	}

}
//我的面板
class MyPanel extends JPanel implements KeyListener{
	//定义一个我的tank
	Hero hero=null;
	Vector<EnemyTank> enemyTanks =new Vector<EnemyTank>();
	int enSize = 3;
	
	public MyPanel(){
		hero = new Hero(200, 200);
		//初始化敌人的坦克
		for(int i=0;i<enSize;i++){
			EnemyTank enemyTank = new EnemyTank((i+1)*50, 0);
			enemyTanks.add(enemyTank);
		}
		System.out.println(enemyTanks.size());
	}
	public void paint(Graphics g) {
		super.paint(g);
		g.fillRect(0, 0, 400, 300);
		this.drawTank(hero.getX(),hero.getY(), g, hero.getDirect(), 0);
		for(int i=0; i<enemyTanks.size();i++){
			this.drawTank(enemyTanks.get(i).getX(), enemyTanks.get(i).getY(), g, enemyTanks.get(i).getDirect(), 1);
		}

	}
	public void drawTank(int x, int y,Graphics g, int direct,int type) {
		switch (type) {
		case 0:
			g.setColor(Color.cyan);
			break;
		case 1:
			g.setColor(Color.yellow);
		}
	
		//判断方向
		switch (direct) {
		case 1:
			g.fill3DRect(x, y, 5, 30,false);
			g.fill3DRect(x+15, y, 5, 30,false);
			g.fill3DRect(x+5, y+5, 10, 20,false);
			g.fillOval(x+5, y+10, 10, 10);
			g.drawLine(x+10, y+15, x+10, y);
			break;
		case 2:
			g.fill3DRect(x, y, 5, 30,false);
			g.fill3DRect(x+15, y, 5, 30,false);
			g.fill3DRect(x+5, y+5, 10, 20,false);
			g.fillOval(x+5, y+10, 10, 10);
			g.drawLine(x+10, y+15, x+10, y+30);
			break;
		case 3:
			g.fill3DRect(x, y, 30, 5,false);
			g.fill3DRect(x, y+15, 30, 5,false);
			g.fill3DRect(x+5, y+5, 20, 10,false);
			g.fillOval(x+10, y+5, 10, 10);
			g.drawLine(x, y+10, x+15, y+10);
			break;
			
		case 4:
			g.fill3DRect(x, y, 30, 5,false);
			g.fill3DRect(x, y+15, 30, 5,false);
			g.fill3DRect(x+5, y+5, 20, 10,false);
			g.fillOval(x+10, y+5, 10, 10);
			g.drawLine(x+15, y+10, x+30, y+10);
			break;
			
		}
		
		
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO 自动生成的方法存根
		
	}
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO 自动生成的方法存根
		if(e.getKeyCode() == KeyEvent.VK_W){
			this.hero.setDirect(1);
			this.hero.moveup();
			System.out.println("向上"+this.hero.y);
		}else if(e.getKeyCode() == KeyEvent.VK_S){
			this.hero.setDirect(2);
			this.hero.moveDown();
			System.out.println("向下");
		}else if(e.getKeyCode() == KeyEvent.VK_A){
			this.hero.setDirect(3);
			this.hero.moveLeft();
			System.out.println("向左");
		}else if(e.getKeyCode() == KeyEvent.VK_D){
			this.hero.setDirect(4);
			this.hero.moveRight();
			System.out.println("向右");
		}
		this.repaint();
		
		
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO 自动生成的方法存根
		
	}
}


